package top.pmwly.oauth2_jwt.service;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Herther
 * @version 1.0.0
 * @ClassName UserService.java
 * @Description 自定登录逻辑
 * @createTime 2021年08月21日 17:16:00
 */
@Service
public class UserService implements UserDetailsService {

    /**
     *  注入passwordencode
     */
    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * 重写loadUserByUsername 方法
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String password = passwordEncoder.encode("123456");
        System.out.println(password);
        //放入admin角色
        return new User(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
